package com.avic.rong.adapter.show;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.avic.rong.R;
import com.avic.rong.adapter.PublicNumberAdapter;
import com.avic.rong.bean.show_bean.EsInfoBean;
import com.avic.rong.bean.show_bean.ShowBean;
import com.avic.rong.bean.show_bean.TypeBean;

import java.util.List;

/**
 * Created by TSJ-2018-0492 on 2019/8/9.
 */

public class FollowListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater inflater;
    private List<ShowBean> bean;
    private Context context;
    private int TYPE_7 = 7, TYPE_8 = 8, TYPE_9 = 9, TYPE_3 = 3;
    private List<EsInfoBean> EsData;
    private List<TypeBean> data;


    public FollowListAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setBean(List<ShowBean> bean) {
        this.bean = bean;
//        notifyDataSetChanged();
    }

    public void setEsData(List<EsInfoBean> esData) {
        EsData = esData;
//        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.roofplacement_ment, viewGroup, false);
        ;
        if (i == TYPE_7) {
            view = inflater.inflate(R.layout.my_follow_rec, viewGroup, false);
            return new BigRecommecdViewHolder(view);
        } else if (i == TYPE_8) {
            view = inflater.inflate(R.layout.roofplacement_ment, viewGroup, false);
            return new BigRecommecdViewHolder(view);
        } else if (i == TYPE_3) {
            view = inflater.inflate(R.layout.roofplacement_ment, viewGroup, false);
            return new BigRecommecdViewHolder(view);
        } else if (i == TYPE_9) {
            view = inflater.inflate(R.layout.kong_layout, viewGroup, false);
            return new BigRecommecdViewHolder(view);
        } else if (i == 0) {
            view = inflater.inflate(R.layout.kong_layout, viewGroup, false);
            return new BigRecommecdViewHolder(view);
        }

        return new BigRecommecdViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        BigRecommecdViewHolder holder = (BigRecommecdViewHolder) viewHolder;
        if (i == 0) {
            for (int j = 0; j < bean.size(); j++) {
                if (bean.get(j) != null) {
                    if (bean.get(j).getType() != null) {
                        Integer integer = Integer.valueOf(bean.get(j).getType());
                        if (integer == TYPE_7) {
                            List<TypeBean> data = bean.get(j).getData();
                            // 横向列表关注   需要在第一行
                            PublicNumberAdapter publicNumberAdapter = new PublicNumberAdapter(context);
                            LinearLayoutManager libraryLayoutManager = new LinearLayoutManager(context);
                            libraryLayoutManager.setOrientation(OrientationHelper.HORIZONTAL);
                            publicNumberAdapter.setData(data);
                            holder.type_7_rec.setLayoutManager(libraryLayoutManager);
                            holder.type_7_rec.setAdapter(publicNumberAdapter);
                        }
                    }
                }
            }

        }


        int itemViewType = getItemViewType(i);
        if (bean != null) {
            if (bean.get(i) != null) {
                if (bean.get(i).getType() != null) {
                    switch (itemViewType) {
                        case 3:
                            data = bean.get(i).getData();
                            TypethreeAdapter followAdapter = new TypethreeAdapter(context);
                            followAdapter.setData(data);
                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                            holder.roofplacement_rec.setLayoutManager(linearLayoutManager);
                            holder.roofplacement_rec.setAdapter(followAdapter);

                            break;
                        case 8:
                            data = bean.get(i).getData();
                            TransverseAdapter transverseAdapter = new TransverseAdapter(context, data, EsData);
                            LinearLayoutManager libraryLayoutManager = new LinearLayoutManager(context);
                            holder.roofplacement_rec.setLayoutManager(libraryLayoutManager);
                            holder.roofplacement_rec.setAdapter(transverseAdapter);

                            break;
                        case 9:
                            data = bean.get(i).getData();
                            TypethreeAdapter typethreeAdapter = new TypethreeAdapter(context);
                            typethreeAdapter.setData(data);
                            holder.roofplacement_rec.setLayoutManager(new LinearLayoutManager(context));
                            holder.roofplacement_rec.setAdapter(typethreeAdapter);

                            break;
                        default:
                            return;
                    }
                }
            }
        } else {
            Toast.makeText(context, "数据空", Toast.LENGTH_LONG).show();
        }


//            if (bean.get(i) != null) {
//                if (bean.get(i).getType()!=null) {
//                    if (Integer.valueOf(bean.get(i).getType()) == TYPE_3) {
//                        for (int j = 0; j < bean.size(); j++) {
//                            if (bean.get(j) != null) {
//                                if (bean.get(j).getType() != null) {
//                                    if (Integer.valueOf(bean.get(j).getType()) == TYPE_3) {
//                                        holder.all_follow.setVisibility(View.VISIBLE);
//                                        List<TypeBean> data = bean.get(j).getData();
//                                        TypethreeAdapter followAdapter = new TypethreeAdapter(context);
//                                        followAdapter.setData(data);
//                                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
//                                        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//                                        holder.roofplacement_rec.setLayoutManager(linearLayoutManager);
//                                        holder.roofplacement_rec.setAdapter(followAdapter);
//                                    }
//                                }
//                            }
//                        }
//
//                    } else if (Integer.valueOf(bean.get(i).getType()) == TYPE_8) {
//                        for (int j = 0; j < bean.size(); j++) {
//                            if (bean.get(j) != null) {
//                                if (bean.get(j).getType() != null) {
//                                    if (Integer.valueOf(bean.get(j).getType()) == TYPE_8) {
//                                        List<TypeBean> data = bean.get(j).getData();
//                                        TransverseAdapter followAdapter = new TransverseAdapter(context, data, EsData);
//                                        LinearLayoutManager libraryLayoutManager = new LinearLayoutManager(context);
//                                        libraryLayoutManager.setOrientation(OrientationHelper.VERTICAL);
//                                        holder.roofplacement_rec.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
//                                        holder.roofplacement_rec.setLayoutManager(libraryLayoutManager);
//                                        holder.roofplacement_rec.setAdapter(followAdapter);
//                                    }
//                                }
//                            }
//
//                        }
//
//                    } else if (Integer.valueOf(bean.get(i).getType()) == TYPE_9) {
//                        for (int j = 0; j < bean.size(); j++) {
//                            if (bean.get(j) != null) {
//                                if (bean.get(j).getType() != null) {
//                                    if (Integer.valueOf(bean.get(j).getType()) == TYPE_9) {
//                                        List<TypeBean> data = bean.get(j).getData();
//                                        TypethreeAdapter followAdapter = new TypethreeAdapter(context);
//                                        followAdapter.setData(data);
//                                        holder.roofplacement_rec.setLayoutManager(new LinearLayoutManager(context));
//                                        holder.roofplacement_rec.setAdapter(followAdapter);
//                                    }
//                                }
//                            }
//                        }
//
//
//                    }
//                }
//            } else {
//                Toast.makeText(context, "数据空", Toast.LENGTH_LONG).show();
//            }
    }


    @Override
    public int getItemCount() {
        if (bean != null) {
//         for (int i = 0; i < bean.size(); i++) {
//            if (bean.get(i) != null) {
//                count++;
//            }
//        }

            return bean.size();
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return TYPE_7;
        }
        //八种布局格式
        if (bean != null) {
            if (bean.get(position) != null) {
                if (bean.get(position).getType() != null) {
                    String type = bean.get(position).getType();
                    int i1 = Integer.parseInt(type);
                    switch (i1) {
                        case 8:
                            return TYPE_8;
                        case 3:
                            return TYPE_3;
                        case 9:
                            return TYPE_9;
                    }
                }

            }
        }else{
            return 0;
        }

        return super.getItemViewType(position);
    }
}
